package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowInfoStatusEnum;
import com.atguigu.srb.core.mapper.BorrowInfoMapper;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author Mr.bilal
 * @since 2023-07-18
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    DictService dictService;

    @Resource
    BorrowerMapper borrowerMapper;

    @Autowired
    BorrowerService borrowerService;

    @Autowired
    LendService lendService;

    @Override
    public Integer getBorrowInfoStatusByUserId(String userId) {
        QueryWrapper<BorrowInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        BorrowInfo borrower = baseMapper.selectOne(queryWrapper);
        if (borrower == null) {
            return 0;
        }
        return borrower.getStatus();
    }

    @Override
    public void saveBorrowInfo(BorrowInfo borrowInfo, String userId) {
        borrowInfo.setBorrowYearRate(borrowInfo.getBorrowYearRate().divide(new BigDecimal("100")));
        borrowInfo.setUserId(Long.valueOf(userId));
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());
        baseMapper.insert(borrowInfo);
    }

    @Override
    public List<BorrowInfo> selectList() {

        List<BorrowInfo> borrowInfos = baseMapper.selectList(null);
        for (BorrowInfo borrowInfo : borrowInfos) {
            Long userId = borrowInfo.getUserId();
            UserInfo userInfo = userInfoMapper.selectById(userId);
            borrowInfo.getParam().put("name",userInfo.getName());
            borrowInfo.getParam().put("mobile",userInfo.getMobile());
            borrowInfo.getParam().put("returnMethod", dictService.getDictNameByDictCodeAndValueId("returnMethod",borrowInfo.getReturnMethod()));
            borrowInfo.getParam().put("moneyUse", dictService.getDictNameByDictCodeAndValueId("moneyUse",borrowInfo.getMoneyUse()));
            borrowInfo.getParam().put("status", BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus()));
        }
        return borrowInfos;
    }

    @Override
    public Map<String, Object> show(Long borrowInfoId) {
        HashMap<String, Object> borrowInfoDetail = new HashMap<>();
        // 根据借款id查询借款人详情
        BorrowInfo borrowInfo = baseMapper.selectById(borrowInfoId);
        Long userId = borrowInfo.getUserId();
        QueryWrapper<Borrower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Borrower borrower = borrowerMapper.selectOne(queryWrapper);

        BorrowerDetailVO borrowerVO = borrowerService.show(borrower.getId());
        borrowerVO.setIncome(dictService.getDictNameByDictCodeAndValueId("income",borrower.getIncome()));
        borrowInfoDetail.put("borrower",borrowerVO);

        // 查询借款详情
        borrowInfo.getParam().put("returnMethod", dictService.getDictNameByDictCodeAndValueId("returnMethod", borrowInfo.getReturnMethod()));
        borrowInfo.getParam().put("moneyUse", dictService.getDictNameByDictCodeAndValueId("moneyUse", borrowInfo.getMoneyUse()));
        borrowInfo.getParam().put("status", BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus()));
        borrowInfoDetail.put("borrowInfo",borrowInfo);

        return borrowInfoDetail;
    }

    @Override
    public void approvalSubmit(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        //修改借款信息状态
        Long id = borrowInfoApprovalVO.getId();
        BorrowInfo borrowInfo = baseMapper.selectById(id);
        borrowInfo.setStatus(borrowInfoApprovalVO.getStatus());
        baseMapper.updateById(borrowInfo);

        //审核通过则创建标的
        if (borrowInfoApprovalVO.getStatus().intValue() == BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue()) {
            //创建标的
            lendService.createLend(borrowInfoApprovalVO,borrowInfo);
        }
    }
}
